package com.qf.shop.dao.impl;

import com.qf.shop.dao.TypeDao;
import com.qf.shop.entity.Type;
import com.qf.shop.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class TypeDaoImpl implements TypeDao{
    QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
    @Override
    public List<Type> selectAll() throws SQLException {

        String sql = "select t_id as tid,t_name as tname ,t_info as tinfo from type limit 5;";
        return queryRunner.query(sql, new BeanListHandler<Type>(Type.class));
    }

    @Override
    public int deleteTypeByTid(String tid) {
        String sql = "delete from type where t_id=?;";
        try {
            return queryRunner.update(sql,tid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateTypeByTid(Type type) {
        String sql = "update from type set t_name=?,t_info=? where t_id=?;";
        try {
            return queryRunner.update(sql,type.getTname(),type.getTinfo(),type.getTid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int insertType(String tname, String tinfo) {
        String sql = "insert into type values(?,?,?)";
        try {
            return queryRunner.update(sql,null,tname,tinfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
